//
//  ZYCTeaLeaveFooterCollectionCell.m
//  ZDSC
//
//  Created by 张威 on 2021/11/3.
//

#import "ZYCTeaLeaveFooterCollectionCell.h"
#import "ZYCTeaLeaveHeaderView.h"
#import "ZYCTeaLeaveHeaderCell.h"
#import "ZYCAddPhotoPlaceHolderCell.h"

@interface ZYCTeaLeaveFooterCollectionCell()<UICollectionViewDataSource,UICollectionViewDelegate,ZYCTeaLeaveHeaderCellDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIView *line;


@end
@implementation ZYCTeaLeaveFooterCollectionCell


- (void)setModel:(ZYCTeaLeaveFlowChartModel *)model{
    _model = model;
    [self.collectionView reloadData];
    if (model.isShow) {
        _line.hidden = NO;
    }else{
        _line.hidden = YES;
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.collectionView];

        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(24.0f);
            make.width.mas_equalTo(2.0f);
            make.top.bottom.equalTo(self.contentView);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.line.mas_right).offset(27.0f);
            make.top.bottom.right.equalTo(self.contentView);
        }];
 
    }
    return self;
}


#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([self.model.nodeType integerValue] == 3) {
        return self.model.applyUserList.count + 1;
    }else{
        return self.model.applyUserList.count;
    }

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.model.applyUserList.count) {
        ZYCAddPhotoPlaceHolderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class]) forIndexPath:indexPath];
        return cell;
    }else{
        ZYCTeaLeaveHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCTeaLeaveHeaderCell class]) forIndexPath:indexPath];
        cell.model = self.model.applyUserList[indexPath.item];
        cell.delegate = self;
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.model.applyUserList.count) {
        if ([self.delegate respondsToSelector:@selector(teaLeaveFooterCollectionCellDidAddApprover:)]) {
            [self.delegate teaLeaveFooterCollectionCellDidAddApprover:self.model];
        }
    }
}

#pragma mark - ZYCTeaLeaveHeaderCellDelegate
- (void)teaLeaveHeaderCellDeleteApprover:(ZYCTeaLeaveApplyUserModel *)m{
    [self.model.applyUserList removeObject:m];
    [self.collectionView reloadData];
    
    if ([self.delegate respondsToSelector:@selector(teaLeaveFooterCollectionCellDidDeleteApprover:)]) {
        [self.delegate teaLeaveFooterCollectionCellDidDeleteApprover:m];
    }
}

#pragma mark - getters
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[ZYCTeaLeaveHeaderCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCTeaLeaveHeaderCell class])];
        [_collectionView registerClass:[ZYCAddPhotoPlaceHolderCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class])];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(70.0f, 70.0f);
        _layout.minimumLineSpacing = 0;//行间距
//        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumInteritemSpacing = 0;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _layout;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#BFC7D0");
        _line.alpha = 0.36f;
    }
    return _line;
}

@end
